package com.good.ui;

import com.good.dao.tUserDao;
import com.good.po.tUser;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;

public class loginUi extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					loginUi frame = new loginUi();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public loginUi() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 680, 445);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("用户名:");
		lblNewLabel.setBounds(65, 91, 118, 18);
		contentPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(188, 88, 327, 24);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("密码:");
		lblNewLabel_1.setBounds(63, 159, 72, 18);
		contentPane.add(lblNewLabel_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(188, 156, 327, 24);
		contentPane.add(passwordField);
		
		JButton btnNewButton = new JButton("登录");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String name = textField.getText();
				String pwd = passwordField.getText();
				System.out.println("=============name:" + name + "------------:" + pwd);
				if(null == name || name.equals("") || null == pwd || pwd.equals("")){
					JOptionPane.showMessageDialog(null, "请输入用户名或密码");
				}else{
					tUserDao tud = new tUserDao();
					ArrayList<tUser> list = tud.selectById(name);
					if(list.size() > 0){
						tUser tu = list.get(0);
						if(tu.getPwd().equals(pwd)){
							mainUi.main(null);
							dispose();
						}else{
							JOptionPane.showMessageDialog(null, "用户名或者密码不正确,请重新输入");
							passwordField.setText("");
						}
					}else{
						registerUi.main(null);
						dispose();
					}
				}
			}
		});
		btnNewButton.setBounds(123, 260, 134, 27);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("注册");
		btnNewButton_1.setBounds(357, 260, 113, 27);
		contentPane.add(btnNewButton_1);
	}
}
